﻿using Microsoft.EntityFrameworkCore;
using MT.Common;
using MT.DAL.Infrastructure;
using MT.Models.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MT.DAL.Dto.UserCenter
{
    public class UserLoginLogDto : RepositoryBase<UserLoginLog>
    {
        public UserLoginLogDto(AppDbContext dbContext) : base(dbContext)
        {
        }

        public async Task<PageModel<UserLoginLog>> GetPageAsync(int pageIndex, int pageSize, string name, DateTime? startTime, DateTime? endTime)
        {
            var result = DataContext.UserLoginLog.AsQueryable();
            if (!string.IsNullOrWhiteSpace(name))
            {
                result = result.Join(DataContext.User.Where(s => s.Name.Contains(name)), uo => uo.UserId, u => u.Id, (uo, u) => uo);
            }
            if (startTime != null)
            {
                result = result.Where(f => f.CreateTime >= startTime);
            }

            if (endTime != null)
            {
                result = result.Where(f => f.CreateTime < endTime);
            }
            result = result.Where(f => f.Delete == false);
            result = result.OrderByDescending(x => x.CreateTime);
            var count = result.Count();
            var list =await  result.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
            return new PageModel<UserLoginLog>(pageIndex, pageSize, list, count);
        }

    }
}
